package cool.goodbai.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cool.goodbai.entity.Employ;
import cool.goodbai.entity.Post;
import cool.goodbai.entity.User;
import cool.goodbai.example.EmployExample;
import cool.goodbai.handler.DataProxy;
import cool.goodbai.mapper.EmployMapper;
import cool.goodbai.service.EmployService;
import cool.goodbai.util.StringUtil;
import cool.goodbai.vo.EmployHXYDto;
import cool.goodbai.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class EmployServiceImpl implements EmployService {
    @Autowired
    private EmployMapper employMapper;

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private PostServiceImpl postService;

    @Autowired
    private MailServiceImpl mailService;

    @Autowired
    private DataProxy dataProxy;

    @Override
    public Employ insert(Employ employ) throws IOException {
        EmployExample employExample = new EmployExample();
        employExample.createCriteria().andUserIdEqualTo(Long.valueOf(StpUtil.getLoginId().toString()));
        if (employMapper.selectByExample(employExample).size() != 0) {
            throw new IOException();
        }
        dataProxy.preInsert(employ);
        employMapper.insertSelective(employ);
        mailService.sendMail(employ);
        return employMapper.selectByPrimaryKey(employ.getId());
    }

    @Override
    public Employ delete(Long id) {
        Employ employ = employMapper.selectByPrimaryKey(id);
        if (employ != null) {
            employMapper.deleteByPrimaryKey(id);
            return employ;
        } else {
            throw new RuntimeException();
        }
    }

    @Override
    public Employ update(Employ employ) {
        Employ old = employMapper.selectByPrimaryKey(employ.getId());
        if (old != null) {
            dataProxy.preUpdate(employ);
            employMapper.updateByPrimaryKeySelective(employ);
            mailService.sendMail(employMapper.selectByPrimaryKey(employ.getId()));
            return employ;
        } else {
            throw new RuntimeException();
        }
    }

    @Override
    public PageResult<List<EmployHXYDto>> select(Employ employ, Integer current, Integer pageSize) {
        EmployExample example = new EmployExample();
        EmployExample.Criteria criteria = example.createCriteria();
        //用户名
        if (!Objects.isNull(employ.getId())) {
            criteria.andIdEqualTo(employ.getId());
        }
        //用户id
        if (!Objects.isNull(employ.getUserId())) {
            criteria.andUserIdEqualTo(employ.getUserId());
        }
        //岗位id
        if (!Objects.isNull(employ.getPostId())) {
            criteria.andPostIdEqualTo(employ.getPostId());
        }
        //审核状态
        if (!Objects.isNull(employ.getStatus())) {
            criteria.andStatusEqualTo(employ.getStatus());
        }
        //第一次审核人
        if (StringUtil.isNotEmpty(employ.getFirstPeople())) {
            criteria.andFirstPeopleLike("%" + employ.getFirstPeople() + "%");
        }
        //第二次安排人
        if (StringUtil.isNotEmpty(employ.getSecondPeople())) {
            criteria.andSecondPeopleLike("%" + employ.getSecondPeople() + "%");
        }
        //第三次面试官
        if (StringUtil.isNotEmpty(employ.getThirdPeople())) {
            criteria.andThirdPeopleLike("%" + employ.getThirdPeople() + "%");
        }
        //第四次审查官
        if (StringUtil.isNotEmpty(employ.getForthPeople())) {
            criteria.andForthPeopleLike("%" + employ.getForthPeople() + "%");
        }

        long total = employMapper.countByExample(example);
        if (current != null && pageSize != null) {
            example.setLimit(pageSize);
            example.setOffset(((current.longValue() - 1) * pageSize));
        }
        List<Employ> employs = employMapper.selectByExample(example);
        List<EmployHXYDto> hxydb = new ArrayList<>();
        employs.forEach(e -> {
            EmployHXYDto dto = new EmployHXYDto();

            User user = new User();
            user.setId(e.getUserId());
            dto.setUserId(userService.select(user, 1, 1).getData().get(0));

            Post post = new Post();
            post.setId(e.getPostId());
            dto.setPostId(postService.select(post, 1, 1).getData().get(0));

            if (StringUtil.isNotEmpty(e.getFirstPeople())) {
                user.setId(Long.valueOf(e.getFirstPeople()));
                dto.setFirstPeople(userService.select(user, 1, 1).getData().get(0));
                dto.setFirstTime(e.getFirstTime());
            }
            if (StringUtil.isNotEmpty(e.getSecondPeople())) {
                user.setId(Long.valueOf(e.getSecondPeople()));
                dto.setSecondPeople(userService.select(user, 1, 1).getData().get(0));
                dto.setSecondTime(e.getSecondTime());
            }
            if (StringUtil.isNotEmpty(e.getThirdPeople())) {
                user.setId(Long.valueOf(e.getThirdPeople()));
                dto.setThirdPeople(userService.select(user, 1, 1).getData().get(0));
                dto.setThirdEvaluate(e.getThirdEvaluate());
                dto.setThirdTime(e.getThirdTime());
            }
            if (StringUtil.isNotEmpty(e.getForthPeople())) {
                user.setId(Long.valueOf(e.getForthPeople()));
                dto.setForthPeople(userService.select(user, 1, 1).getData().get(0));
                dto.setForthTime(e.getForthTime());
            }

            if (!Objects.isNull(e.getTime())) {
                dto.setTime(e.getTime());
            }
            if (!Objects.isNull(e.getStatus())) {
                dto.setStatus(e.getStatus());
            }
            if (!Objects.isNull(e.getId())) {
                dto.setId(e.getId());
            }
            if (!Objects.isNull(e.getUtcCreate())) {
                dto.setUtcCreate(e.getUtcCreate());
            }
            if (!Objects.isNull(e.getUtcModify())) {
                dto.setUtcModify(e.getUtcModify());
            }
            if (StringUtil.isNotEmpty(e.getCreateBy())) {
                dto.setCreateBy(e.getCreateBy());
            }
            if (StringUtil.isNotEmpty(e.getModifyBy())) {
                dto.setModifyBy(e.getModifyBy());
            }
            if (!Objects.isNull(e.getValid())) {
                dto.setValid(e.getValid());
            }
            if (StringUtil.isNotEmpty(e.getRemark())) {
                dto.setRemark(e.getRemark());
            }
            hxydb.add(dto);
        });
        return new PageResult<>(hxydb, current, pageSize, true, total, 0);
    }

    @Override
    public Employ selectByUserId(Long userId) {
        EmployExample employExample = new EmployExample();
        employExample.createCriteria().andUserIdEqualTo(userId);
        return employMapper.selectByExample(employExample).get(0);
    }
}
